package com.liuxvru.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuxvru.common.utils.SecurityUtils;
import com.liuxvru.project.domain.Release;
import com.liuxvru.project.domain.Sprint;
import com.liuxvru.project.domain.Work;
import com.liuxvru.project.mapper.ReleaseMapper;
import com.liuxvru.project.mapper.SprintMapper;
import com.liuxvru.project.mapper.WorkMapper;
import com.liuxvru.project.service.IReleaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 发布Service业务层处理
 *
 * @author liuxvru
 * @date 2025-02-10
 */
@Service
public class ReleaseServiceImpl extends ServiceImpl<ReleaseMapper, Release> implements IReleaseService {
    @Autowired
    WorkMapper workMapper;
    @Autowired
    SprintMapper sprintMapper;

    /**
     * 查询发布列表
     *
     * @param release 发布
     * @return 发布
     */
    @Override
    public List<Release> selectReleaseList(Release release) {
        return baseMapper.selectReleaseList(release);
    }

    @Override
    public int add(Long projectId, Release release) {
        Long userId = SecurityUtils.getUserId();
        release.setProjectId(projectId);
        release.setCreatedBy(userId);
        release.setUpdatedBy(userId);
        return baseMapper.insert(release);
    }

    @Override
    public int edit(Long projectId, Release release) {
        Long userId = SecurityUtils.getUserId();
        Release release1 = baseMapper.selectById(release.getReleaseId());
        if (release1 == null || !release1.getProjectId().equals(projectId)) {
            throw new RuntimeException("修改发布出错");
        }
        release.setUpdatedBy(userId);
        return baseMapper.updateById(release);
    }

    @Override
    @Transactional
    public int addSprint(Long releaseIds, Long sprintId) {
        Release release = baseMapper.selectById(releaseIds);
        Sprint sprint = sprintMapper.selectById(sprintId);
        if (release == null || sprint == null || !release.getProjectId().equals(sprint.getProjectId()) || sprint.getReleaseId() != null) {
            throw new RuntimeException("添加迭出错");
        }
        Long userId = SecurityUtils.getUserId();
        sprintMapper.update(null, new LambdaUpdateWrapper<Sprint>().set(Sprint::getUpdatedBy,userId).set(Sprint::getReleaseId, releaseIds).eq(Sprint::getSprintId, sprintId));
        workMapper.update(null, new LambdaUpdateWrapper<Work>().set(Work::getUpdatedBy,userId).set(Work::getReleaseId, releaseIds).eq(Work::getSprintId, sprintId));
        return 1;
    }

    @Override
    @Transactional
    public int removeSprint(Long releaseIds, Long sprintId) {
        Release release = baseMapper.selectById(releaseIds);
        Sprint sprint = sprintMapper.selectById(sprintId);
        if (release == null || sprint == null || !release.getProjectId().equals(sprint.getProjectId())|| !Objects.equals(sprint.getReleaseId(), releaseIds)) {
            throw new RuntimeException("移除迭代出错");
        }
        Long userId = SecurityUtils.getUserId();
        sprintMapper.update(null, new LambdaUpdateWrapper<Sprint>().set(Sprint::getUpdatedBy,userId).set(Sprint::getReleaseId, null).eq(Sprint::getSprintId, sprintId));
        workMapper.update(null, new LambdaUpdateWrapper<Work>().set(Work::getUpdatedBy,userId).set(Work::getReleaseId, null).eq(Work::getSprintId, sprintId));
        return 1;
    }


}